package str

import (
	"fmt"
	"strings"
)

// LeftPad fills left side of the string with filled characters
func LeftPad(s string, pad int, fill string) string {
	var sb strings.Builder

	if fill == "" {
		fill = " "
	}

	sb.Grow(pad)

	for i := 0; i < pad-len(s); i++ {
		sb.WriteString(fill)
	}

	sb.WriteString(s)

	return sb.String()
}

// leftPad fills the string with filled characters
// not efficient implementation for benchmarking
func leftPad(s string, pad int, fill string) string {
	formatStr := fmt.Sprintf("%%%s%ds", fill, pad)
	return fmt.Sprintf(formatStr, s)
}

// RightPad fills right side of the string with filled characters
// Example:
// str.RightPad("1", 5, "0")
// "10000"
func RightPad(s string, pad int, fill string) string {
	var sb strings.Builder

	if fill == "" {
		fill = " "
	}

	sb.Grow(pad)
	sb.WriteString(s)

	for i := 0; i < pad-len(s); i++ {
		sb.WriteString(fill)
	}

	return sb.String()
}
